package cn.nawang.ebeim.ui.controller;

import cn.nawang.ebeim.client.dto.ClientDto;
import cn.nawang.ebeim.client.service.ClientService;
import cn.nawang.ebeim.ui.bean.User;
import cn.nawang.ebeim.ui.util.DirectoryUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;

/**
 * 临时用controller ，放置一些ui搬过来的代码
 * Created by GanJc on 2016/2/1.
 */
@RestController
@RequestMapping(value = "/api",produces="application/json;charset=UTF-8")
public class TempController {

    private static final Logger LOG = LoggerFactory.getLogger(TempController.class);

    @Resource
    private ClientDto dto ;

    @Resource
    private ClientService clientService ;

    @RequestMapping(value = "/system/dirs", method = RequestMethod.GET)
    public String getDiskDirs(String path,String action) {
        LOG.info("getDiskFiles: {}", path);
        JSONObject json = new JSONObject();
        json.put("code", "200");
        boolean isRoot = path == null || path.isEmpty();
        if (!isRoot) {
            File exist = new File(path);
            if (!exist.exists()) {
                json.put("code", "500");
                json.put("message", "系统找不到该路径:" + path);
                return json.toString();
            }
        }
        File[] files = DirectoryUtil.getDirs(path, isRoot);

        List<FileInfo> list = createFileInfos(Arrays.asList(files), isRoot);
        Gson gson = new Gson();
        String data = gson.toJson(list);
        LOG.info("files:{}", data);
        return data;
    }

    private List<FileInfo> createFileInfos(List<File> files, boolean isRoot) {
        List<FileInfo> list = new ArrayList<FileInfo>();
        for (File f : files) {
            String name = isRoot ? f.getPath() : f.getName();
            list.add(new FileInfo(name,f.getAbsolutePath(), f.isDirectory()));
        }
        return list;
    }

    private static class FileInfo{
        private String name;
        private boolean isParent;
        private String absolutePath;
        public String getAbsolutePath() {
            return absolutePath;
        }
        public String getName() {
            return name;
        }
        public boolean isParent() {
            return isParent;
        }
        public FileInfo(String name, String absolutePath, boolean isParent) {
            this.name = name;
            this.isParent = isParent;
            this.absolutePath = absolutePath;
        }
    }

    @RequestMapping(value = "/userinfo", method = RequestMethod.GET)
    public String getUserinfo() {
        User user = new User();
        // String rs = ApiUtil.login(user);
        JSONObject json = new JSONObject();
        json.put("user", user);
        return json.toJSONString();
    }

    @RequestMapping(value = "/datasources/save", method = RequestMethod.POST)
    public String saveDataSource(String path) {
        LOG.info("saveDataSource - {}", path);
        String  dsId = "DS-" + new Date().getTime() + "-" + new Random().nextInt(1000);
        clientService.addUploadProcess(dsId,path);
        JSONObject json = new JSONObject();
        json.put("code", "200");
        return json.toJSONString();
    }

}
